// components/post-card/post-card.js
Component({
  properties: {
    post: {
      type: Object,
      value: {},
    },
    postIndex: {
      type: Number,
      value: 0,
    },
    column: {
      type: String,
      value: "",
    },
  },

  data: {
    showActionSheet: false,
    showShareMenu: false,
  },

  lifetimes: {
    attached() {
      // 组件加载时检查点赞状态
      this.checkLikeStatus();
    },
  },

  methods: {
    // 卡片点击事件
    onCardTap() {
      wx.navigateTo({
        url: `/pages/detail/detail?id=${this.data.post.id}`,
      });
    },

    // 预览图片
    previewImage(e) {
      const current = e.currentTarget.dataset.url;
      const urls = e.currentTarget.dataset.urls;

      wx.previewImage({
        current,
        urls,
      });
    },

    // 用户头像点击
    onUserTap() {
      const userId = this.data.post.user.id;
      this.triggerEvent("user-tap", { userId });
    },

    // 点赞点击
    onLikeTap() {
      const postId = this.data.post.id;
      const postIndex = this.data.postIndex;
      const column = this.data.column;

      this.triggerEvent("like", {
        postId,
        postIndex,
        column,
      });
    },

    // 评论点击
    onCommentTap() {
      wx.navigateTo({
        url: `/pages/detail/detail?id=${this.data.post.id}&tab=comments`,
      });
    },

    // 分享点击
    onShareTap() {
      this.triggerEvent("share", {
        postId: this.data.post.id,
      });
    },

    // 更多操作点击
    onMoreTap() {
      this.setData({
        showActionSheet: true,
      });
    },

    // 关闭操作面板
    closeActionSheet() {
      this.setData({
        showActionSheet: false,
      });
    },

    // 举报帖子
    reportPost() {
      this.closeActionSheet();
      wx.showModal({
        title: "举报",
        content: "确定要举报这条内容吗？",
        success: (res) => {
          if (res.confirm) {
            wx.showToast({
              title: "举报成功",
              icon: "success",
            });
          }
        },
      });
    },

    // 复制链接
    copyLink() {
      this.closeActionSheet();
      wx.setClipboardData({
        data: `https://petcommunity.com/post/${this.data.post.id}`,
        success: () => {
          wx.showToast({
            title: "链接已复制",
            icon: "success",
          });
        },
      });
    },

    // 检查点赞状态
    checkLikeStatus() {
      const post = this.data.post;
      if (post && post.id) {
        // 从本地存储获取点赞状态
        const isLiked = wx.getStorageSync(`like_${post.id}`) || false;
        this.setData({
          [`post.isLiked`]: isLiked,
        });
      }
    },

    // 格式化数字
    formatNumber(num) {
      if (num >= 10000) {
        return (num / 10000).toFixed(1) + "w";
      } else if (num >= 1000) {
        return (num / 1000).toFixed(1) + "k";
      }
      return num.toString();
    },

    // 格式化时间
    formatTime(timeStr) {
      const now = new Date();
      const time = new Date(timeStr);
      const diff = now - time;

      const minute = 60 * 1000;
      const hour = 60 * minute;
      const day = 24 * hour;
      const week = 7 * day;
      const month = 30 * day;

      if (diff < minute) {
        return "刚刚";
      } else if (diff < hour) {
        return Math.floor(diff / minute) + "分钟前";
      } else if (diff < day) {
        return Math.floor(diff / hour) + "小时前";
      } else if (diff < week) {
        return Math.floor(diff / day) + "天前";
      } else if (diff < month) {
        return Math.floor(diff / week) + "周前";
      } else {
        return time.toLocaleDateString();
      }
    },

    // 获取用户等级图标
    getUserLevelIcon(level) {
      const levelIcons = {
        1: "🥉",
        2: "🥈",
        3: "🥇",
        4: "👑",
        5: "💎",
      };
      return levelIcons[level] || "🥉";
    },
  },
});
